package com.lineage.server.command.executor;

import com.lineage.server.datatables.DeNameTable;
import com.lineage.server.datatables.NpcTable;
import com.lineage.server.model.Instance.L1DeInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.DeName;
import com.lineage.server.templates.L1Npc;
import com.lineage.server.thread.GeneralThreadPool;
import com.lineage.server.utils.L1SpawnUtil;
import java.util.StringTokenizer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class L1SpawnDe implements L1CommandExecutor {
  private static final Log _log = LogFactory.getLog(L1SpawnDe.class);
  
  public static L1CommandExecutor getInstance() {
    return new L1SpawnDe();
  }
  
  public void execute(L1PcInstance pc, String cmdName, String arg) {
    try {
      if (arg.startsWith("obj")) {
        StringTokenizer stringTokenizer = new StringTokenizer(arg);
        int objid = Integer.parseInt(stringTokenizer.nextToken());
        int time = 0;
        try {
          time = Integer.parseInt(stringTokenizer.nextToken());
        } catch (Exception exception) {}
        int i = 81471;
        L1Npc l1Npc = NpcTable.get().getTemplate(81471);
        if (l1Npc != null) {
          DeName de = DeNameTable.get().getDeName(objid);
          L1SpawnUtil.spawnDE(pc, de, true, 5, time);
        } 
        return;
      } 
      if (arg.startsWith("s")) {
        boolean isShopMap = false;
        switch (pc.getMapId()) {
          case 340:
          case 350:
          case 360:
          case 370:
          case 800:
            isShopMap = true;
            break;
        } 
        if (isShopMap) {
          L1DeInstance de2 = L1SpawnUtil.spawnDE(pc, null, true, 5, 0);
          de2.start_shop();
        } else {
          pc.sendPackets((ServerBasePacket)new S_SystemMessage("目前位置不在商店街。"));
        } 
        return;
      } 
      StringTokenizer tok = new StringTokenizer(arg);
      int count = 1;
      if (tok.hasMoreTokens())
        count = Integer.parseInt(tok.nextToken()); 
      int randomrange = 1;
      if (tok.hasMoreTokens())
        randomrange = Integer.parseInt(tok.nextToken(), 10); 
      int npcid = 81471;
      L1Npc npc = NpcTable.get().getTemplate(81471);
      if (npc == null) {
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("找不到该npc。"));
        return;
      } 
      if (count > 100) {
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("一次召唤数量不能超过100。"));
        return;
      } 
      if (count > 20) {
        DeRunnable deRunnable = new DeRunnable(pc, 81471, randomrange, count);
        GeneralThreadPool.get().execute(deRunnable);
      } else {
        int i = 0;
        while (i < count) {
          L1SpawnUtil.spawnDE(pc, null, false, randomrange, 0);
          i++;
        } 
      } 
      String msg = String.format("%s(%d) (%d) 召唤。 (范围:%d)", new Object[] { npc.get_name(), Integer.valueOf(81471), 
            Integer.valueOf(count), Integer.valueOf(randomrange) });
      pc.sendPackets((ServerBasePacket)new S_SystemMessage(msg));
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
      _log.error("错误的GM指令格式: " + getClass().getSimpleName() + " 执行的GM:" + pc.getName());
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(261));
    } 
  }
  
  private class DeRunnable implements Runnable {
    private final L1PcInstance _pc;
    
    private final int _npcid;
    
    private final int _randomrange;
    
    private final int _count;
    
    private DeRunnable(L1PcInstance pc, int npcid, int randomrange, int count) {
      this._pc = pc;
      this._npcid = npcid;
      this._randomrange = randomrange;
      this._count = count;
    }
    
    public void run() {
      try {
        int i = 0;
        while (i < this._count) {
          if (this._pc.getMapId() == 5300) {
            L1SpawnUtil.spawnDE(this._pc, null, false, 5, 0);
          } else {
            L1SpawnUtil.spawn(this._pc, this._npcid, this._randomrange, 0);
          } 
          Thread.sleep(1L);
          i++;
        } 
      } catch (InterruptedException e) {
        L1SpawnDe._log.error(e.getLocalizedMessage(), e);
      } 
    }
  }
}
